/**
 * http://usejsdoc.org/
 */
var formidable = require("formidable"),
	http = require("http"),
	util =require("util");
/**应用HTTP的createServer方法创建服务器*/
http.createServer(function(req,res){
	/**判断请求路径是否为upload,如果是则执行文件上传代码逻辑*/
	if(req.url === '/upload' && req.method.toLowerCase() === 'post'){
		// parse file upload
		var form = new formidable.IncomingForm();//创建form对象
		form.parse(req, function(err,fields,files) {//解析post数据
			res.writeHead( 200 , {"Content-Type":"text/plain"});
			res.write("received upload:\n\n");
			res.end(util.inspect({fields:fields,files:files}));//将json对象转成字符串
		});
		return;
	}
	//show a file upload
	res.writeHead( 200 , {"Content-Type":"text/html"});
	/**响应http信息*/
	res.end(
			'<form action="/upload" enctype="multipart/form-data" method="post">'+
			'<input type="text" name="title"/><br/>'+
			'<input type="file" name="upload" multiple="multiple"/><br/>'+
			'<input type="submit" value="Upload"/><br/>'+
			'</form>'
	);
}).listen(1400,'127.0.0.1');